package com.dly.blog.controller;

import com.dly.blog.annotation.OpLog;
import com.dly.blog.domain.ro.UserCreateRO;
import com.dly.blog.domain.ro.user.UserAddRO;
import com.dly.blog.domain.ro.user.UserEditRO;
import com.dly.blog.domain.ro.user.UserPageRO;
import com.dly.blog.domain.vo.*;
import com.dly.blog.msg.R;
import com.dly.blog.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: DaiLiYu
 * @Date: 2023/7/16 19:54
 */
@RestController
@RequestMapping("/user")
@AllArgsConstructor
@Tag(name="用户相关")
@OpLog("用户相关的信息")
public class UserController extends BaseController{

    private final UserService userService;

    @GetMapping("/admin_info")
    @Operation(summary = "获取用户信息")
    @PreAuthorize("hasRole('admin')")
    public ResponseEntity<R<AdminUserVO>> getAdminUserInfo(){
        return operate(()->this.successDate(userService.getAdminUserInfo()));
    }

    @GetMapping("/info")
    @Operation(summary = "获取用户信息")
    public ResponseEntity<R<UserVO>> getUserInfo(){
        return operate(()->this.successDate(userService.getUserInfo()));
    }

    @PostMapping(value = "/page")
    @Operation(summary = "分页获取用户信息")
    public ResponseEntity<R<PageVO<UserVO>>> getUserPage(@RequestBody UserPageRO pageRO){
        return operate(()-> this.successDate(userService.getUserPage(pageRO)));
    }

    @PostMapping("/create")
    @Operation(summary = "管理员创建用户接口")
    @PreAuthorize("hasRole('admin')")
    public ResponseEntity<R<Void>> create(
            @RequestBody UserCreateRO userCreateRO
    ) {
        userService.create(userCreateRO);
        return operate(this::success);
    }

    @GetMapping("/reset/{id}")
    @Operation(summary = "管理员创建用户接口")
    @PreAuthorize("hasRole('admin')")
    public ResponseEntity<R<Void>> reset(@PathVariable("id") String id) {
        userService.reset(id);
        return operate(this::success);
    }

    @PostMapping(value = "/add")
    @Operation(summary = "添加用户表信息")
    public ResponseEntity<R<Void>> addUser(@RequestBody UserAddRO userAddRO){
        userService.addUser(userAddRO);
        return operate(this::success);
    }

    @PostMapping(value = "/edit")
    @Operation(summary = "修改用户表信息")
    public ResponseEntity<R<Void>> editUser(@RequestBody UserEditRO userEditRO){
        userService.editUser(userEditRO);
        return operate(this::success);
    }

    @PostMapping(value = "/get/{id}")
    @Operation(summary = "获取用户表信息")
    public ResponseEntity<R<UserVO>> getUser(@PathVariable(value = "id") String id){
        return operate(()-> this.successDate(userService.getUser(id)));
    }

    @PostMapping(value = "/remove/{ids}")
    @Operation(summary = "删除用户表信息")
    public ResponseEntity<R<Void>> removeUser(@PathVariable(value = "ids") String[] ids){
        userService.removeUser(ids);
        return operate(this::success);
    }
}
